package com.lagou.edu.controller;

import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import com.lagou.edu.vo.LoginVo;
import com.lagou.edu.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: ResumeController
 * @Auther: Jerry
 * @Date: 2020/7/19 13:43
 * @Desctiption: TODO
 * @Version: 1.0
 */
@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    @RequestMapping("/list")
    @ResponseBody
    public ResultInfo list() throws Exception {
        List<Resume> resumeList = resumeService.findAll();
        return ResultInfo.success(resumeList);
    }

    @RequestMapping("/page")
    @ResponseBody
    public ResultInfo page(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) throws Exception {
        Page<Resume> page = resumeService.findByPage(pageNo - 1, pageSize);
        return ResultInfo.success(page);
    }

    @RequestMapping("/add")
    public String add() throws Exception {
        return "add";
    }

    @RequestMapping("/save")
    @ResponseBody
    public ResultInfo save(@RequestBody Resume resume) throws Exception {
        boolean flag = resumeService.save(resume);
        return ResultInfo.success(flag);
    }

    @RequestMapping("/update")
    @ResponseBody
    public ResultInfo update(@RequestBody Resume resume) throws Exception {
        boolean flag = resumeService.save(resume);
        return ResultInfo.success(flag);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo delete(@RequestParam("id") Long id) throws Exception {
        resumeService.delete(id);
        return ResultInfo.success("删除成功");
    }

    @GetMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @GetMapping("/main")
    public String main() {
        return "main";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo login(@RequestBody LoginVo loginVo,
                            HttpSession session) throws Exception {
        ResultInfo resultInfo = resumeService.login(loginVo.getUsername(), loginVo.getPassword());
        session.setAttribute("user", resultInfo.getData());
        return resultInfo;
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) throws Exception {
        session.invalidate();
        return "redirect:toLogin";
    }

}
